package com.frxuexi.shop2307.shop

import androidx.lifecycle.MutableLiveData
import com.frxuexi.lib_base.entity.CouponBean
import com.frxuexi.lib_base.entity.HomeBean
import com.frxuexi.lib_base.entity.ProductBean
import com.frxuexi.lib_base.entity.ProductBeanItem
import com.frxuexi.lib_base.entity.ProductInfo
import com.frxuexi.lib_base.entity.RechargeBean
import com.frxuexi.lib_base.entity.ScoreBean
import com.frxuexi.lib_base.entity.ShopBean
import com.frxuexi.lib_base.entity.ShopCommentBean
import com.frxuexi.lib_base.entity.ShopCommentNumBean
import com.frxuexi.lib_base.entity.TypeShopBean
import com.frxuexi.lib_base.http.ApiService
import com.frxuexi.lib_base.mvvm.BaseViewModel
import com.frxuexi.lib_base.room.ProductDao
import com.frxuexi.lib_base.room.ProductEntity
import com.frxuexi.lib_base.room.SearchDao
import com.frxuexi.lib_base.room.SearchEntity
import com.frxuexi.lib_base.room.YueDao
import com.frxuexi.lib_base.room.YueEntity
import com.frxuexi.lib_base.utils.SignLiveData
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class ShopViewModel @Inject constructor(val apiService: ApiService,val searchDao: SearchDao,val productDao: ProductDao,val yueDao: YueDao):BaseViewModel(){

    val homeData=SignLiveData<HomeBean>()
    val shopError=SignLiveData<String>()
    fun getHomes(){
        httpRequest({apiService.getHomes()},homeData::postValue,shopError::postValue)
    }

    val productData = MutableLiveData<ProductBean>()

    fun getProducts(type:Int,page:Int){
        httpRequest({apiService.getProducts(type, page)},productData::postValue,{
            shopError.postValue(it)
        })
    }

    val productTypeData = MutableLiveData<ProductBean>()

    fun getProductsByType(type:Int,page:Int){
        httpRequest({apiService.getProductsByType(type, page)},productTypeData::postValue,{
            shopError.postValue(it)
        })
    }

    val productTwoTypeData = MutableLiveData<TypeShopBean>()
    fun getProductsByTwoType(){
        httpRequest({apiService.getProductsByTwoType()},productTwoTypeData::postValue,shopError::postValue)
    }

    val detailData=MutableLiveData<ShopBean>()
    fun getShopDetail(id:Int){
        httpRequest({apiService.getShopDetail(id)},detailData::postValue,shopError::postValue)
    }

    val commenNumData=SignLiveData<ShopCommentNumBean>()
    fun getCommentNum(id:Int){
        httpRequest({apiService.getCommentNum(id)},commenNumData::postValue,shopError::postValue)
    }

    val commentData=SignLiveData<ShopCommentBean>()
    fun getComment(id:Int,limit:Int, page:Int, type:String){
        httpRequest({apiService.getComments(id,limit, page, type)},commentData::postValue,shopError::postValue)
    }

    val shopKeyData=SignLiveData<ProductBean>()
    fun getShopByKey(cid:Int?=0,keyword:String,limit:Int,page:Int){
        httpRequest({apiService.getShopByKey(cid!!,keyword, limit, page)},shopKeyData::postValue,shopError::postValue)
    }

    val addSearchData=SignLiveData<Long>()
    fun addSearch(searchEntity: SearchEntity){
        roomRequest({searchDao.addSearch(searchEntity)},addSearchData::postValue,shopError::postValue)
    }

    val searchData=SignLiveData<List<SearchEntity>>()
    fun getSearchs(){
        roomRequest({searchDao.getSearchs()},searchData::postValue,shopError::postValue)
    }

    val deleteSearchData=SignLiveData<Int>()
    fun deleteSearch(searchEntity: SearchEntity){
        roomRequest({searchDao.deleteSearch(searchEntity)},deleteSearchData::postValue,shopError::postValue)
    }

    val addProData=SignLiveData<Long>()
    fun addPro(productEntity: ProductEntity){
        roomRequest({productDao.addPro(productEntity)},addProData::postValue,shopError::postValue)
    }

    val isLoveData=SignLiveData<ProductEntity>()
    fun isLove(id:Int){
        roomRequest({productDao.isLove(id)},isLoveData::postValue,shopError::postValue)
    }

    val deleteLoveData=SignLiveData<Int>()
    fun deleteLove(productEntity: ProductEntity){
        roomRequest({productDao.deleteLove(productEntity)},deleteLoveData::postValue,shopError::postValue)
    }

    val prosData=SignLiveData<List<ProductEntity>>()
    fun getProsLove(){
        roomRequest({productDao.getPros()},prosData::postValue,shopError::postValue)
    }

    val addCartData=SignLiveData<Any>()
    fun addCart(map: Map<String,Any>){
        httpRequest({apiService.addCart(toBody(map))},addCartData::postValue,shopError::postValue)
    }

    val couponsData=SignLiveData<CouponBean>()
    fun getCoupons(){
        httpRequest({apiService.getCoupons()},couponsData::postValue,shopError::postValue)
    }

    val receiveCouponData=SignLiveData<String>()
    fun receiveCoupon(map:Map<String,Int>){
        httpRequest({apiService.receiveCoupon(toBody(map))},receiveCouponData::postValue,shopError::postValue)
    }

    val addYueData=SignLiveData<Long>()
    fun addYue(yueEntity: YueEntity){
        roomRequest({yueDao.addYue(yueEntity)},addYueData::postValue,shopError::postValue)
    }
    val selectYueData=SignLiveData<List<YueEntity>>()
    fun selectYue(){
        roomRequest({yueDao.selectYue()},selectYueData::postValue,shopError::postValue)
    }

    val rechargeData=SignLiveData<RechargeBean>()
    fun recharge(){
        httpRequest({apiService.recharge()},rechargeData::postValue,shopError::postValue)
    }
    val scoreData=SignLiveData<ScoreBean>()
    fun getScores(){
        httpRequest({apiService.getScores()},scoreData::postValue,shopError::postValue)
    }
}